﻿using Common.Class;
using Common.Model;
using SqlSugar;

namespace BLL
{
    public class UserBLL
    {
        /// <summary>
        /// 获取操作员列表
        /// </summary>
        /// <returns></returns>
        public static Dictionary<long, UserModel> UserListGet()
        {

            Dictionary<long, UserModel> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<UserModel>().Where(m => m.DeleteFlag == "N").ToList().ToDictionary(m => m.ID.Value);
            return _ml;
        }

        /// <summary>
        /// 根据ID数组获取列表
        /// </summary>
        /// <param name="IDs"></param>
        /// <returns></returns>
        public static Dictionary<long, UserModel> UserListGetFromID(long[] IDs)
        {
            Dictionary<long, UserModel> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<UserModel>().Where(m => IDs.Contains(m.ID.Value)).ToList().ToDictionary(m => m.ID.Value);
            return _ml;
        }

        /// <summary>
        /// 根据用户登录名获取用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static UserModel UserGetFromUserID(string userID)
        {
            UserModel _m = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _m = _db.Queryable<UserModel>().Where(m => m.DeleteFlag == "N" && userID == m.UserID).First();
            return _m;
        }

        /// <summary>
        /// 根据ID获取数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static UserModel UserGetFromID(long ID)
        {
            Dictionary<long, UserModel> _ml = UserListGetFromID(new long[] { ID });
            if (_ml != null && _ml.Count > 0)
            {
                return _ml.Values.ToList()[0];
            }
            return null;
        }

        /// <summary>
        /// 增加操作员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static UserModel UserAdd(UserModel model)
        {
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            model.CreateTime = DateTime.Now;
            UserModel _m = _db.Insertable<UserModel>(model).ExecuteReturnEntity();
            return _m;
        }

        /// <summary>
        /// 更新操作员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool UserUpdate(UserModel model)
        {
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);

            if (_db.Updateable<UserModel>(model).UpdateColumns(model.ChangeProperty.ToArray(), true).ExecuteCommand() > 0)
            {
                model.ChangeProperty = null;
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 获取加密后的密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string GetEncryptPassword(string password)
        {
            return EncryptHelper.EncryptString_MD5(EncryptHelper.EncryptString_MD5(password).ToLower().Substring(0, 16)).ToLower().Substring(0, 16);
        }

        /// <summary>
        /// 增加用户操作日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static UserLogModel UserLogAdd(UserLogModel model)
        {
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            model.CreateTime = DateTime.Now;
            UserLogModel _m = _db.Insertable<UserLogModel>(model).ExecuteReturnEntity();
            return _m;

        }

        /// <summary>
        /// 操作员操作日志列表
        /// </summary>
        /// <param name="beginTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public static List<UserLogModel> UserLogListGet(DateTime beginTime, DateTime endTime)
        {
            List<UserLogModel> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<UserLogModel>().Where(m => m.CreateTime >= beginTime && m.CreateTime <= endTime).ToList();
            return _ml;
        }

        /// <summary>
        /// 获取操作员关联的角色列表
        /// </summary>
        /// <param name="userID">操作员ID</param>
        /// <returns></returns>
        public static List<long> UserRoleListGet(long userID)
        {

            List<long> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<UserRoleModel>().Where(m => m.UserID == userID && m.DeleteFlag == "N").Select(m => m.RoleID.Value).ToList();
            return _ml;
        }

        /// <summary>
        /// 获取操作员关联的会话列表
        /// </summary>
        /// <param name="userID">操作员ID</param>
        /// <returns></returns>
        public static List<SessionModel> UserSessionListGet(long userID)
        {

            List<long> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            _ml = _db.Queryable<UserRoleModel>().Where(m => m.UserID == userID && m.DeleteFlag == "N").Select(m => m.RoleID.Value).ToList();

            _ml = _db.Queryable<RoleSessionModel>().Where(m => _ml.Contains(m.RoleID.Value) && m.DeleteFlag == "N").Select(m => m.SessionID.Value).ToList();

            List<SessionModel> _sessionList = SessionBLL.SessionListGet();
            SessionCheck(_sessionList, _ml);
            return _sessionList;
        }

        public static bool SessionCheck(List<SessionModel> _modelList, List<long> _sessionIDs)
        {
            bool _isCheck = false;
            for (int i = _modelList.Count - 1; i >= 0; i--)
            {
                SessionModel _model = _modelList[i];
                if (_sessionIDs.Contains(_model.ID))
                {
                    _model.IsCheck = true;
                    _isCheck = true;
                }
                if (_model.ChildSessionList.Count > 0)
                {
                    if (SessionCheck(_model.ChildSessionList, _sessionIDs))
                    {
                        _model.IsCheck = true;
                        _isCheck = true;
                    }
                }
                if (!_model.IsCheck)
                {
                    _modelList.Remove(_model);
                }
            }
            return _isCheck;
        }

        /// <summary>
        /// 操作员与角色关联
        /// </summary>
        /// <param name="userID">操作员ID</param>
        /// <param name="roleIDs">角色ID列表</param>
        /// <returns></returns>
        public static List<long> UserRoleListAdd(long userID, List<long> roleIDs)
        {

            List<long> _ml = null;
            string _connnectStr = BLL.ConfigBLL.DBConfigConnectString;
            SqlSugarClient _db = SugarHelper.GetSqlSugarClient(BLL.ConfigBLL.DBType, _connnectStr, CommonBLL.DBRunTimeOut, CommonBLL.IsDBLog, false);
            try
            {
                _db.BeginTran();
                //废弃历史关联
                _db.Updateable<UserRoleModel>().SetColumns(it => new UserRoleModel() { DeleteFlag = "Y" }).Where(m => m.UserID == userID && m.DeleteFlag == "N").ExecuteCommand();
                //新增关联
                foreach (long roleid in roleIDs)
                {
                    UserRoleModel _userRole = new UserRoleModel();
                    _userRole.ID = CommonHelper.GetNewID();
                    _userRole.UserID = userID;
                    _userRole.RoleID = roleid;
                    _userRole.CreateTime = DateTime.Now;
                    _userRole.DeleteFlag = "N";
                    _db.Insertable<UserRoleModel>(_userRole).ExecuteReturnEntity();
                }

                _db.CommitTran();
            }
            catch (Exception ex)
            {
                _db.RollbackTran();
                throw ex;
            }


            return _ml;
        }

    }
}
